<template>
	<view class="" style="width: 100%;">

		<button @click="option" class=""
			style="display: inline-block; width: 160rpx; height: 160rpx;  text-align: center;  padding: 40rpx; box-sizing: border-box;"
			v-show="srcList.length < num">
			<u-icon name="plus" size="40"></u-icon>

		</button>
		<view class="img_view" v-for="(item, index) of fileList" :key="item"
			style="float: left; width: 160rpx; height: 160rpx; position: relative; overflow: hidden;">
			<u-icon name="close-circle-fill" @click="clearimg(item, index)" size="28"
				style="position: absolute; z-index: 33; top: 0%; right: 0%; transform: translate(26%, -26%)"></u-icon>
			<image :showLoading="true" :src="item" mode="" style="width: 100%; height: 100%;" @click="sebigImg(item)">
			</image>
		</view>
		<view
			style="width: 160rpx; height: 160rpx; border-radius: 10rpx; margin-right: 10rpx;  float: left; position: relative; overflow: hidden;"
			v-for="(item, index) of videoList" :key="item">
			<!-- close-circle -->
			<u-icon name="close-circle-fill" @click="clearVide(item, index)" size="28"
				style="position: absolute; z-index: 33; top: 0%; right: 0%; transform: translate(26%, -26%)"></u-icon>
			<video :show-center-play-btn="false" :controls="false" :src="item"
				style="width: 100%; height: 100%; border-radius: 10rpx;" @click="seeVideo(item)"></video>
		</view>

		<u-overlay :show="videoIsShow" @click="videoIsShow = false">
			<view class=""
				style="height: 100vh; width: 100vw;display: flex; justify-content: center; align-content: center;">
				<video :src="videoSrc" style="width: 100%; margin: auto;" :opacity="0.7"></video>
			</view>
		</u-overlay>
		<u-popup :show="show" @close="show = false" @open="show = true" style="width: 100%;">
			<view class="" style="width: 100%; padding: 50rpx 0; border-radius: 20rpx 20rpx; height: auto;">
				<u-upload @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="10"
					style="width: 100%;" v-if="type == 'img' || type =='all'">
					<u-cell-group style="width: 100vw">
						<u-cell style="width: 100%;">
							<template slot="title">
								<view style="width: 100%;">选择图片</view>
							</template>
							<u-icon name="arrow-right" slot="right-icon"></u-icon>
						</u-cell>
					</u-cell-group>
				</u-upload>
				<u-upload @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="10"
					style="width: 100%;" accept="video" v-if="type == 'video' || type == 'all'">
					<u-cell-group style="width: 100vw">
						<u-cell style="width: 100%;">
							<template slot="title">
								<view style="width: 100%;">选择视频</view>
							</template>
							<u-icon name="arrow-right" slot="right-icon"></u-icon>
						</u-cell>
					</u-cell-group>
				</u-upload>
			</view>

		</u-popup>
		<u-modal :show="showModal" :title="title" :showCancelButton="true" @confirm="confirm" @cancel="showModal=false">
			<view class="slot-content">
				<rich-text :nodes="content"></rich-text>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import {
		baseURL
	} from "@/request/index.js"

	export default {
		data() {
			return {
				isCLlick: this.$store.state.isClicked,
				show: false,
				showModal: false,
				title: '权限申请',
				content: `我们需要访问您的相机和存储权限，以便您可以上传图片。这些权限仅用于实现应用的正常功能，并确保您的使用体验。具体说明如下：
存储权限：我们需要访问您的存储空间，以便保存您拍摄的照片。这将帮助您管理和查看您拍摄的所有照片，并且您可以随时访问和分享这些图片。
请放心，我们尊重您的隐私，所有的图片和数据仅用于应用功能，不会泄露给第三方。感谢您的理解和支持！`,
				fileList: [],
				videoSrc: "",
				videoIsShow: false,
				videoList: [],
				srcList: []
			}
		},
		props: {
			type: {
				type: String,
				default: "all"
			},
			num: {
				type: [Number, String],
				default: 3
			}
		},
		computed() {
			
		},
		// mounted() {
		// 	console.log(this.isCLlick);
		// },
		methods: {
			option() {
		  
				if(!this.$store.state.isClicked) {
					this.showModal = true
				}else {
					this.show = true
				}

			},
			confirm() {
				// this.isCLlick = true
				this.$store.commit('changeClickState', true)
				this.show = true
				this.showModal = false
			},
			clearimg(srcitem) {
				this.fileList = this.fileList.filter((item, index) => {
					return srcitem != item
				})
				this.srcList = this.srcList.filter((item, index) => {
					return srcitem != item
				})
				this.$emit("change", this.srcList)
			},
			clearVide(srcitem) {
				this.videoList = this.videoList.filter((item, index) => {
					return srcitem != item
				})
				this.srcList = this.srcList.filter((item, index) => {
					return srcitem != item
				})
				this.$emit("change", this.srcList)
			},

			sebigImg(src) {
				uni.previewImage({
					current: src,
					urls: this.fileList,
				})
			},
			seeVideo(src) {
				this.videoIsShow = true;
				this.videoSrc = src
			},
			beforeRead() {

			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {

				if (event.file[0].size > 200000000) {
					return uni.showToast({
						title: '不得超过200MB',
						icon: 'none'
					})
				}
				let lists = [].concat(event.file)
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					if (result.code != 200) {
						uni.showToast({
							title: '上传失败',
							icon: 'error'
						})
					} else {
						if (lists[i].type != "image") {
							this.videoList.push(result.data)
						} else {
							this.fileList.push(result.data)
						}
						this.srcList.push(result.data)
						this.$emit("change", this.srcList)
					}
					this.show = false

				}


			},

			uploadFilePromise(url) {
				let loadding = uni.showLoading({
					title: '上传中',
					mask: true
				})
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: baseURL + '/face/fileup',
						filePath: url,
						name: 'file',
						success: (res) => {
							resolve(JSON.parse(res.data))
							uni.hideLoading(loadding)
						},
						fail: () => {
							uni.hideLoading(loadding)
						}
					});
				})
			},
		}

	}
</script>

<style lang="scss" scoped>
	::v-deep .u-upload__wrap {
		width: 100% !important;
	}

	::v-deep .u-cell__body {
		width: 100% !important;
	}

	.slot-content {
		font-size: 32rpx;
	}
</style>